﻿#include "GenBaseKItemControl.h"
#include "OptionManager.h"
#include <QDir>
#include "PriceFileToKItemFIle.h"

GenBaseKItemControl::GenBaseKItemControl(QListWidget *symbolList, QObject *parent)
	: QObject(parent)
	, m_symbolList(symbolList)
{
}

GenBaseKItemControl::~GenBaseKItemControl()
{
}

void GenBaseKItemControl::GenAll()
{
	if (m_symbolList == nullptr)
	{
		return;
	}
	PriceFileToKItemFIle fileKItem(nullptr);
	for (int i = 0; i < m_symbolList->count(); ++i)
	{
		QListWidgetItem *pItem = m_symbolList->item(i);
		if (pItem->checkState() != Qt::Checked)
		{
			continue;
		}
		QStringList mdates;
		getAllSymbolDayList(mdates);
		for (QStringList::iterator itDay = mdates.begin(); itDay != mdates.end(); ++itDay)
		{
			fileKItem.CompositingKItemByDay(pItem->text(), *itDay);
		}
	}
}

void GenBaseKItemControl::GenAllPrice()
{
	PriceFileToKItemFIle fileKItem(nullptr);
	QStringList mdateDirs;
	getAllSymbolDirList(mdateDirs);
	for (QStringList::iterator itDay = mdateDirs.begin(); itDay != mdateDirs.end(); ++itDay)
	{
		QStringList symbolList;
		getAllSymbolNameList(*itDay, symbolList);
		QFileInfo pfInfo(*itDay);
		QString pday = pfInfo.fileName();
		for (QStringList::iterator itSymbol = symbolList.begin(); itSymbol != symbolList.end(); ++itSymbol)
		{
			if (fileKItem.HasGenBaseKItem(*itSymbol, pday))
			{
				continue;
			}
			fileKItem.CompositingKItemByDay(*itSymbol, pday);
		}
	}
}

void GenBaseKItemControl::getAllSymbolDayList(QStringList &outDirList)
{
	std::string stdDir = OptionManager::Instance().GetMarketDataDir();
	QDir dir(QString::fromLocal8Bit(stdDir.c_str()));
	//要判断路径是否存在
	if (!dir.exists())
	{
		return;
	}
	dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);//实现对文件的过滤

	QFileInfoList list = dir.entryInfoList();
	QStringList string_list;

	for (int i = 0; i < list.size(); ++i)
	{
		QFileInfo fileInfo = list.at(i);
		outDirList << fileInfo.fileName();
	}
}

void GenBaseKItemControl::getAllSymbolDirList(QStringList &outDirList)
{
	std::string stdDir = OptionManager::Instance().GetMarketDataDir();
	QDir dir(QString::fromLocal8Bit(stdDir.c_str()));
	//要判断路径是否存在
	if (!dir.exists())
	{
		return;
	}
	dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);//实现对文件的过滤

	QFileInfoList list = dir.entryInfoList();
	QStringList string_list;

	for (int i = 0; i < list.size(); ++i)
	{
		QFileInfo fileInfo = list.at(i);
		outDirList << fileInfo.absoluteFilePath();
	}
}

void GenBaseKItemControl::getAllSymbolNameList(const QString &pDir, QStringList &outSymbolList)
{
	QDir dir(pDir);
	//要判断路径是否存在
	if (!dir.exists())
	{
		return;
	}
	dir.setFilter(QDir::Files);//实现对文件的过滤

	QFileInfoList list = dir.entryInfoList();
	QStringList string_list;

	for (int i = 0; i < list.size(); ++i)
	{
		QFileInfo fileInfo = list.at(i);
		outSymbolList << fileInfo.baseName();
	}
}
